package com.boda.common.util.str;

import java.util.List;
import java.util.Map;

public class StringHelper {
    public String[] Split(String strSplit, String splitFlag) {
        if (strSplit == null || splitFlag == null) {
            String[] tmpSplit = new String[1];
            tmpSplit[0] = strSplit;
            return tmpSplit;
        }

        java.util.StringTokenizer st = new java.util.StringTokenizer(strSplit, splitFlag);

        int size = st.countTokens();
        String[] tmpSplit = new String[size];

        for (int i = 0; i < size; i++) {
            tmpSplit[i] = st.nextToken();
        }
        return tmpSplit;
    }

    public String ArrayToString(String[] strArray, String splitFlag) {
        String tmpString = "";
        if (strArray.length == 0) {
            tmpString = "";
        } else {
            for (int i = 0; i < strArray.length; i++) {
                tmpString = tmpString + strArray[i];
                if (i < strArray.length - 1) {
                    tmpString = tmpString + splitFlag;
                }
            }
        }
        tmpString = tmpString.trim();
        return tmpString;
    }

    public String ArrayToString2(String[] strArray, String splitFlag) {
        String tmpString = "";
        if (strArray.length == 0) {
            tmpString = "''";
        } else {
            for (int i = 0; i < strArray.length; i++) {
                tmpString = tmpString + "'" + strArray[i] + "'";
                if (i < strArray.length - 1) {
                    tmpString = tmpString + splitFlag;
                }
            }
        }
        tmpString = tmpString.trim();
        return tmpString;
    }

    public static String getFileExt(String fileName) {
        String value = new String();
        int start = 0;
        int end = 0;
        if (fileName == null)
            return null;
        start = fileName.lastIndexOf(46) + 1;
        end = fileName.length();
        value = fileName.substring(start, end);
        if (fileName.lastIndexOf(46) > 0)
            return value;
        else
            return "";
    }

    public boolean isNumeric(String strData, boolean dotFlag) {
        if (strData == null) {
            return false;
        }
        char[] numbers = strData.toCharArray();
        for (int i = 0; i < numbers.length; i++) {
            if (dotFlag) {
                if (!Character.isDigit(numbers[i]))
                    return false;
            } else {
                if (!Character.isDigit(numbers[i]) && numbers[i] != '.')
                    return false;
            }
        }
        if (strData.lastIndexOf(46) != strData.indexOf(46))
            return false;
        return true;
    }

    public static String changeChinese(String chnString) {
        String strChinese = null;
        byte[] temp;
        if (chnString == null || chnString == "") {
            return new String("");
        }
        try {
            temp = chnString.getBytes("ISO-8859-1");
            strChinese = new String(temp);
        } catch (java.io.UnsupportedEncodingException e) {
            System.out.println(e);
        }
        return strChinese;
    }

    public static String changeSybase(String chnString) {
        String strChinese = null;
        byte[] temp;
        if (chnString == null || chnString == "") {
            return new String("");
        }
        try {
            temp = chnString.getBytes("gb2312");
            strChinese = new String(temp);
        } catch (java.io.UnsupportedEncodingException e) {
            System.out.println(e);
        }
        return strChinese;
    }

    public static String Replace(String source, String oldString, String newString) {
        StringBuffer output = new StringBuffer();

        int lengthOfSource = source.length();
        int lengthOfOld = oldString.length();

        int posStart = 0;
        int pos;

        while ((pos = source.indexOf(oldString, posStart)) >= 0) {
            output.append(source.substring(posStart, pos));

            output.append(newString);
            posStart = pos + lengthOfOld;
        }
        if (posStart < lengthOfSource) {
            output.append(source.substring(posStart));
        }
        return output.toString();
    }

    public static String Left(String sourceString, int nLength) {
        if (sourceString == null || sourceString == "" || sourceString.length() <= nLength) {
            return sourceString;
        }
        return sourceString.substring(0, nLength);
    }

    public static String Reverse(String strReverse) {
        if (strReverse == null) {
            return strReverse;
        } else {
            StringBuffer tmpString = new StringBuffer(strReverse);

            tmpString = tmpString.reverse();

            return tmpString.toString();
        }
    }

    public static String Right(String sourceString, int nLength) {
        if (sourceString == null || sourceString == "" || sourceString.length() <= nLength) {
            return sourceString;
        }
        return sourceString.substring(sourceString.length() - nLength, sourceString.length());
    }

    public static String Mid(String sourceString, int nStart, int nLength) {
        try {
            if (sourceString == null || sourceString == "") {
                return sourceString;
            }
            int Length = sourceString.length();
            if (nStart > Length || nStart < 0) {
                return null;
            }
            if ((nStart + nLength) > Length)
                return sourceString.substring(nStart, Length);
            return sourceString.substring(nStart, nStart + nLength);
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    public static String toSql(String str) {
        String sql = new String(str);
        return Replace(sql, "'", "''");
    }

    public static String toHtmlInput(String str) {
        if (str == null)
            return null;

        String html = new String(str);

        html = Replace(html, "&", "&amp;");
        html = Replace(html, "<", "&lt;");
        html = Replace(html, ">", "&gt;");

        return html;
    }

    public static String toHtml(String str) {
        if (str == null) {
            return null;
        }

        String html = new String(str);

        html = toHtmlInput(html);
        html = Replace(html, "\r\n", "\n");
        html = Replace(html, "\n", "<br>");
        html = Replace(html, "\t", "    ");
        html = Replace(html, " ", " &nbsp;");

        return html;
    }

    public static String notEmpty(Object value) {
        if (value == null) {
            value = "";
        }
        return String.valueOf(value);
    }

    public String isSelected(String value, String selectedValue) {
        if (selectedValue.trim().equals("")) {
            selectedValue = value;
        }

        if (selectedValue.trim().equals(value.trim())) {
            return "selected";
        } else {
            return "";
        }
    }

    public String isChecked(String radioValue, String value) {
        if (radioValue.trim().equals(value.trim())) {
            return "checked";
        } else {
            return "";
        }
    }

    public String isChecked(String checkboxValue, String value, String flag) {
        if (flag.length() == 0) {
            flag = ",";
        }

        java.util.StringTokenizer st = new java.util.StringTokenizer(value.trim(), flag);
        int size = st.countTokens();
        boolean checked = false;

        for (int i = 0; i < size; i++) {
            String temp = st.nextToken();
            if (checkboxValue.trim().equals(temp.trim())) {
                return " checked";
            }
        }
        return "";
    }

    public static void main(String[] s) {

    }

    public static String get(Map map, String keyName) {
        return notEmpty(map.get(keyName));
    }

    public void print(Object obj) {
        if (obj instanceof Object[]) {
            Object[] objs = (Object[]) obj;
            for (int i = 0; i < objs.length; i++) {
                System.out.println(i + "=" + objs[i].toString());
            }

        } else
            System.out.println(obj.toString());
    }

    public String getSql(String sql, Object[] params) {
        StringHelper str = new StringHelper();
        int idx = -1;
        int i = 0;
        String tmp = sql;
        while ((idx = tmp.indexOf("?")) > -1) {
            tmp = tmp.replaceFirst("\\?", "'" + str.toSql((String) params[i++]) + "'");
        }
        return tmp;

    }

    public static String toUtf8String(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else {
                byte[] b;
                try {
                    b = Character.toString(c).getBytes("utf-8");
                } catch (Exception ex) {
                    System.out.println(ex);
                    b = new byte[0];
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0)
                        k += 256;
                    sb.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return sb.toString();
    }

    public static String ListToString(List list, String key, String split) {
        String str = "";
        for (int i = 0; i < list.size(); i++) {
            Map map = (Map) list.get(i);
            str += get(map, key) + split;
        }
        if (!"".equals(str))
            str = str.substring(0, str.lastIndexOf(split));
        return str;

    }

    public static String toUTF8(String str) {
        String s = str;
        try {
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < s.length(); i++)
                if (s.charAt(i) > '\177') {
                    result.append("&#x");
                    String hex = Integer.toHexString(s.charAt(i));
                    StringBuffer hex4 = new StringBuffer(hex);
                    hex4.reverse();
                    int len = 4 - hex4.length();
                    for (int j = 0; j < len; j++)
                        hex4.append('0');

                    for (int j = 0; j < 4; j++)
                        result.append(hex4.charAt(3 - j));

                    result.append(';');
                } else {
                    result.append(s.charAt(i));
                }

            return result.toString();
        } catch (Exception e) {
            return s;
        }
    }

}